package MtG.client.panels.cardPanelComponents.gameGards;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import MtG.client.ClientConnection;
import MtG.client.GUI;
import MtG.publicClass.Player;
import MtG.publicClass.cardType.ManaCard;

/**
 * Card.java - A class that draws a card, adds a mouseListener to it.
 * @author Marek Sirp
 * @author Jaan Sepp
 */
public class Card extends JPanel{
	/**
	 * Determines if a de-serialized file is compatible with this class.
	 */
	private static final long serialVersionUID = 7281779681860895001L;
	/**
	 * Field for holding the reference to the card that is to be drawn.
	 */
	private MtG.publicClass.Card currentCard = null;
	/**
	 * Field for holding the cards image.
	 */
	private Image img = null;	
	/**
	 * Field for holding the reference to this exact card.java object.
	 */
	private Card c = this;
	/**
	 * Field for holding the reference to the player, whose card this is.
	 */
	private Player player = null;
	/**
	 * Field for holding the reference to which clientConnection this card belongs.
	 */
	private ClientConnection cc = null;
	/**
	 * Field that holds the color that will highlight the cards when clicked on the table.
	 */
	private Color active = Color.GREEN;
	/**
	 * Card border thickness.
	 */
	private final int LINE = 4;

	
	
	/**
	 * Class constructor.
	 * @param card the card to be drawn.
	 */
	public Card(MtG.publicClass.Card card){
		this.currentCard = card;
		this.addMouseListener(new listenMouse());
		setName("Card");
		try {
			img = ImageIO.read(new File(currentCard.getPicAddress()));
		} catch (IOException e) {
			System.out.println("Could not find " + currentCard.getCardName() + " image.");
		}
		this.setPreferredSize(new Dimension(img.getWidth(null) + LINE, img.getHeight(null) + LINE));
		setToolTipText(currentCard.getCardName() + ". " + currentCard.getCardDescription() + " manacost " + currentCard.getManaCost());	
	}

	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paint(java.awt.Graphics)
	 */
	public void paint(Graphics g){
		super.paint(g);
		setBackground(c.getBackground());
		if(currentCard.isUseThisTurn()){
			setBackground(active);
		} else {
			setBackground(getParent().getBackground());
		}
		g.drawImage(img, LINE/2, LINE/2, null);
	}
	
	/**
	 * Class that holds the instuctions how to act when the mouse is clicked on
	 * the image that uses this listener.
	 * @author Marek Sirp
	 * @author Jaan Sepp
	 */
	public class listenMouse implements MouseListener {
		public void mouseClicked(MouseEvent e) {	
			GUI gui = (GUI)c.getTopLevelAncestor();
			cc = gui.getClientConnection();
			player = cc.getRealPlayer();
			if(cc.getGame().getWhoseTurn().getSecurityNumber().equals(player.getSecurityNumber()) && !player.isSpectator()){
				System.out.println("õigus vajutatada");
					if(e.getClickCount() == 2){
						System.out.println("click counter");
						if( e.getComponent().getParent().getName().equals("CardPanel")){
							System.out.println("Right panel");
							if(currentCard instanceof ManaCard){
								if(!player.isManaPlaced()){
									player.setManaPlaced(true);
									player.setMaxMana(currentCard.getManaCost());
									player.removeCardFromHand(currentCard);
									player.setLastClickedCard(currentCard.getControlCode());
								}							
							} else {
								if(player.getInitMana() - currentCard.getManaCost() >= 0){
									player.setInitMana(player.getInitMana() - currentCard.getManaCost());
									player.removeCardFromHand(currentCard);
									player.addCardToPlay(currentCard);	
									player.setLastClickedCard(currentCard.getControlCode());
								}	

							}	
							cc.sendObject(player);
						}
					} else if (e.getClickCount() == 1 && !( e.getComponent().getParent().getName().equals("CardPanel")) && 
							cc.getGame().getWhoseTurn().getSecurityNumber().equals(player.getSecurityNumber()) && 
							player.getPlayersCardsInPlay().contains(currentCard) && currentCard.isUseable()){
							if(currentCard.isUseThisTurn()){
								currentCard.useOnthisturn(false);
							} else{
								currentCard.useOnthisturn(true);
							}		
							player.setLastClickedCard(currentCard.getControlCode());	
							cc.sendObject(player);
					} 
				
				}
			}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// No need to do anything when mouse enters the surface of this card.
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// No need to do anything when the mouse exits the surface of this card.
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// No need to do anything when the mouse is pressed on this card image.
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// No need to do anything when the mouse is released on the surface of this card.
			
		} 
	}
}
